package zhuyun.code

class ScoreContext {
    companion object {
        @JvmStatic
        fun main(args:Array<String>){
            val scoreContext = ScoreContext()
            scoreContext.add(30)
            scoreContext.add(40)
            scoreContext.add(20)
            scoreContext.add(10)
            scoreContext.add(-15)
            scoreContext.add(-30)
        }
    }
    var state:AbstractState = LowState(this)
    fun add(score:Int){
        state.addScore(score)
    }


}
abstract class AbstractState{
    var hj:ScoreContext? = null
    protected var stateName:String = ""
    var score:Int = 0
    abstract fun checkState()
    fun addScore(x:Int){
        score+=x
        println("加上${x}分，当前分数为$score")
        checkState()
        println("当前状态为:${hj?.state?.stateName}")
    }
}

class HighState():AbstractState(){
    constructor(state:AbstractState) : this() {
        hj = state.hj
        stateName = "优秀"
        score = state.score
    }
    override fun checkState() {
        if(score<60){
            hj?.state = LowState(this)
        } else {
            if(score<90){
                hj?.state  = MiddleState(this)
            }
        }
    }

}
class MiddleState():AbstractState(){
    constructor(state:AbstractState) : this() {
        hj = state.hj
        stateName = "中等"
        score = state.score
    }
    override fun checkState() {
        if(score < 60){
            hj?.state = LowState(this)
        } else {
            if(score >= 90){
                hj?.state = HighState(this)
            }
        }
    }
}

class LowState():AbstractState(){
    constructor(hj:ScoreContext) : this() {
        this.hj = hj
        stateName = "不及格"
        score = 0
    }

    constructor(state:AbstractState) : this() {
        hj = state.hj
        stateName = "不及格"
        score = state.score
    }
    override fun checkState() {
        if(score >= 90){
            hj?.state = HighState(this)
        } else {
            if(score >= 60){
                hj?.state  = MiddleState(this)
            }
        }
    }

}
